<?php
if(!defined('BASE_DIR'))die('Cannot access the file !');
/**
 * -+-----------------------------------------
 * |PHP Framework
 * |Copyright (c) 2010 jinke.[iblue.cc]
 * |E-mail: mejinke@gmail.com
 * |Date:  2010-06-18
 * |Update: 2011-01-06
 * -+-----------------------------------------
 *
 * 系统模板解析类
 * @author jinke
 */
 class Template extends Base
{

	//编译文件保存地址
	public $compile_file	=	 '';

	public function __construct()
	{
		Debug::initClass('template.class.php');
	}

	/**
	 * 编译模板文件
	 * @access private
	 * @param string $tpl_path 模板路径
	 * @param string $tpl_name 模板名称
	 */
	public function compile($tpl_file)
	{

		if(!is_file($tpl_file))
			die($tpl_file);

		$tpl_html	=	Files::read($tpl_file);
		$tpl_html	=	self::preg($tpl_html);


		$sl = "<?php if(!defined('BASE_DIR'))die('Cannot access the file !');?>";
		$tpl_html = $sl."\r\n".$tpl_html;
		//保存编译文件
		Files::write($this->compile_file,$tpl_html,'w+');

		return $tpl_html;
	 }


	private function preg($tpl_html)
	{

		//PHP原生语法 {%php echo'dddd';%}
		//preg_match_all('/\{\%php\%\}(.*)+\{\%\/php\%\}/isU',$tpl_html,$array,PREG_PATTERN_ORDER);
		preg_match_all('/\{\%php(.*)?\%\}/isU',$tpl_html,$array,PREG_PATTERN_ORDER);
		//print_r($array);exit;
		for($i=0;$i<count($array[0]);$i++)
		{
			$tep = $array[0][$i];
			/*$tpl_html = str_replace($tep,str_replace('{%/php%}','	 ?>',str_replace('{%php%}','<?php	',$array[0][$i])),$tpl_html);*/
			 $tpl_html = str_replace($tep,str_replace('%}','	 ?>',str_replace('{%php','<?php	',$array[0][$i])),$tpl_html);
		}
		
		//变量
		preg_match_all('/\{\%\$(.*)+\%\}/isU',$tpl_html,$array,PREG_PATTERN_ORDER);
		for($i=0;$i<count($array[0]);$i++)
		{	
			$tpl_html = str_replace($array[0][$i],'<?php if(isset('.$this->getVar($array[0][$i]).')):echo '.$this->getVar($array[0][$i]).';endif;?>',$tpl_html);
		}
		

		//调用方法
		preg_match_all('/\{\%\=(.*)+\\%}/isU',$tpl_html,$array,PREG_PATTERN_ORDER);
		for($i=0;$i<count($array[0]);$i++)
		{
			$tep = $array[0][$i];
			$tpl_html = str_replace($tep,str_replace('%}',';?>',str_replace('{%=','<?php echo ',$array[0][$i])),$tpl_html);
		}
		
		//匹配标记 <list name="$data" start="0" end="2">
		preg_match_all('/{\%for(\s)+name="(.*)?"(\s)+start="(\d+)"(\s)+end="(\d+)"\%\}/isU',$tpl_html,$array,PREG_PATTERN_ORDER);
		$tpl_html = str_replace('{%/for%}','<?php endfor;?>',$tpl_html);
		for($i=0;$i<count($array[0]);$i++)
		{ 
			$tep = $array[0][$i];
			$tep = str_replace('name="'.$array[2][$i].'"','',$tep);
			$tep = str_replace('start="'.$array[4][$i].'"','',$tep);
			$tep = str_replace('end="'.$array[6][$i].'"','',$tep);
			$tep = str_replace(' ','',$tep);
			$tep = str_replace($tep,str_replace('%}',"):\n if(!isset(".$array[2][$i]."[\$i]))continue;?>",str_replace('{%for','<?php for($i='.$array[4][$i].';$i<='.$array[6][$i].';$i++',$tep)),$tep);
			
			$tpl_html = str_replace($array[0][$i],$tep,$tpl_html);
		}
	
		//匹配标记 <list name="$data" item="v">
		preg_match_all('/{\%list(\s)+name="(.*)?"(\s)+item="(.*)?"\%\}/isU',$tpl_html,$array,PREG_PATTERN_ORDER);
		$tpl_html = str_replace('{%/list%}','<?php endforeach; endif; ?>',$tpl_html);
		for($i=0;$i<count($array[0]);$i++)
		{
			$tep = $array[0][$i];
			$tep = str_replace($tep,str_replace('%}','):?>',str_replace('{%list',"<?php if( isset(".$this->getVar($array[2][$i]).") && is_array(".$this->getVar($array[2][$i]).")):\n foreach(",$array[0][$i])),$tep);
			$tep = str_replace('name="'.$array[2][$i].'"',$this->getVar($array[2][$i]).' as ',$tep);
			$tep = str_replace('item="'.$array[4][$i].'"',$array[4][$i],$tep);
			$tpl_html = str_replace($array[0][$i],$tep,$tpl_html);
		}
		
		
		
	
		//IF ELSE
		preg_match_all('/\{\%if(\s)++(.*)?\%\}/isU',$tpl_html,$array,PREG_PATTERN_ORDER);
		$tpl_html = str_replace('{%/if%}','<?php endif;?>',$tpl_html);
		$tpl_html = str_replace('{%else%}','<?php else: ?>',$tpl_html);
		for($i=0;$i<count($array[0]);$i++)
		{
			$tep = $array[0][$i];
			$tep = str_replace($array[2][$i],'',$tep);
			$tep = str_replace('{%if','<?php if('.$array[2][$i],$tep);
			$tep = str_replace('%}','):?>',$tep);
			$tpl_html = str_replace($array[0][$i],$tep,$tpl_html);
		}

		//RES 全局变量
		preg_match_all('/__(.*)?__/isU',$tpl_html,$array,PREG_PATTERN_ORDER);
		for($i=0;$i<count($array[0]);$i++)
		{
			$tep = $array[0][$i];
			$tep2 = str_replace('__','',$tep);
			$tpl_html =	str_replace($tep,'<?php if(defined(\''.$tep2.'\')){echo '.$tep2.';} ?>',$tpl_html);
		}
		
		//载入模板文件
		preg_match_all('/\{\%import\((.*)?\)\%\}/isU',$tpl_html,$array,PREG_PATTERN_ORDER);
		for($i=0;$i<count($array[0]);$i++)
		{
			$tep = $array[0][$i];
			$tep1 = str_replace('"','' ,str_replace('\'','' ,$array[1][$i]));
			$arr = explode('/',$tep1);
			$tep1 = str_replace($arr[count($arr)-1],'',$tep1);
			
			$tpl_html =	str_replace($tep,'<?php echo $this->FW_View->show("'.$arr[count($arr)-1].'",0,true,"'.$tep1.'");?>',$tpl_html);
		}
		return $tpl_html;
	 }
	 

	 /**
	  * 分析模板变量标记
	  * @access private
	  * @param string $var	变量标记
	  * @return string 
	  */
	 private function getVar($var)
	 {
	 	$var=str_replace('%}', '',str_replace('{%', '', $var));
	 	$array = explode('.',$var);
	 	$var = $array[0];
	 	if (count($array)>1)
	 	{
		 	for ($i = 1; $i < count($array); $i++) {
		 		if(strpos('/'.$array[$i],'$')==1)
		 			$var.='['.$array[$i].']';
		 		else 
		 			$var.='[\''.$array[$i].'\']';
		 	}
	 	}
	 	return $var;
	 }
}
?>